//主控：STM32F103
#include "Dbus_uart.h"
#include "sys.h"
#include "led.h"
#include "usart.h"
/*-----USART1_RX-----PA10----*/
//for D-BUS

volatile unsigned char Dbus_rx_buffer[25];
RC_Ctl_t shuju;
int tmp_buf[25];
u8  TXhc[13];
u8 qhdjsd=0,qhbj=0;
u8 sdbjbj=0;
u8 baohu=0;
u8 bhbj=0;
u8 wuxian_sj=0;
u8 kaiji_jiesuo=0;
void Dbus_Init()
{
    //GPIO端口设置
    GPIO_InitTypeDef	GPIO_InitStructure;
    USART_InitTypeDef	USART_InitStructure;
    
    NVIC_InitTypeDef NVIC_InitStructure;
	DMA_InitTypeDef		dma;
	
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 ,ENABLE);   //使能DMA1
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
   
    USART_DeInit(USART3);  //复位串口1
	
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
    GPIO_Init(GPIOB, &GPIO_InitStructure);  //初始化PA10

    USART_InitStructure.USART_BaudRate = 100000;//波特率100k
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
    USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
    USART_InitStructure.USART_Parity = USART_Parity_Even;//偶校验
    USART_InitStructure.USART_Mode = USART_Mode_Rx;	//接收模式
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
    USART_Init(USART3, &USART_InitStructure); //初始化串口2参数
    USART_Cmd(USART3, ENABLE);                    //使能串口
    USART_DMACmd(USART3,USART_DMAReq_Rx,ENABLE);//开启串口DMA

    DMA_DeInit(DMA1_Channel3);
    dma.DMA_PeripheralBaseAddr = (uint32_t)&(USART3->DR);
    dma.DMA_MemoryBaseAddr = (uint32_t)Dbus_rx_buffer;
    dma.DMA_DIR = DMA_DIR_PeripheralSRC;
    dma.DMA_BufferSize =18;
    dma.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    dma.DMA_MemoryInc = DMA_MemoryInc_Enable;
    dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    dma.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    dma.DMA_Mode = DMA_Mode_Circular;
    dma.DMA_Priority = DMA_Priority_VeryHigh;
    dma.DMA_M2M=DMA_M2M_Disable;
    DMA_Init(DMA1_Channel3,&dma);
 
    NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

 //   DMA1_Channel3->CNDTR=0;
//    DMA_GetCurrDataCounter(DMA_Channel3);
    
    DMA_ITConfig(DMA1_Channel3,DMA_IT_TC,ENABLE);
    DMA_Cmd(DMA1_Channel3,ENABLE);
}
int djsd=15;//目标速度
u8 read_cs=0;
u16  jianpanzhihc[3]={0,0,0};
int chasd=0,xsd=0,ysd=0;
u16 jueduizhi(int jdz)
{
	if(jdz<0)jdz=(-jdz);
	return jdz;
}
u8 anjian_Q=0;
u8 cs_sd=10;//初始速度
void jianpan(void)
{
	        int hc1;
	        if(tmp_buf[4]==3){TXhc[1]=0; TXhc[3]=0;TXhc[5]=0; TXhc[7]=0;}//关闭
		else if(tmp_buf[4]==1)//键鼠
		{ 
				jianpanzhihc[read_cs]=tmp_buf[11];
				if(jianpanzhihc[0]==jianpanzhihc[1]&&jianpanzhihc[1]==jianpanzhihc[2])
				switch (tmp_buf[11])  //按键处理
				{    
					 case KEY_PRESSED_OFFSET_A:     //左平移1
						     if(qhbj!=1) qhdjsd=cs_sd;
						     qhbj=1; 
						     TXhc[1]=qhdjsd;TXhc[2]=0;
						     TXhc[3]=qhdjsd;;TXhc[4]=1;
						     TXhc[5]=qhdjsd;;TXhc[6]=1;
						     TXhc[7]=qhdjsd;;TXhc[8]=0;
						     break;
					  case KEY_PRESSED_OFFSET_D:   //右平移2
						     if(qhbj!=2) qhdjsd=cs_sd;
						     qhbj=2; 
						     TXhc[1]=qhdjsd;TXhc[2]=1;
						     TXhc[3]=qhdjsd;TXhc[4]=0;  
						     TXhc[5]=qhdjsd;TXhc[6]=0;  
						     TXhc[7]=qhdjsd;TXhc[8]=1;
						     break;
					 case KEY_PRESSED_OFFSET_S:     //后退3
						     if(qhbj!=3) qhdjsd=cs_sd;
						     qhbj=3; 
						     TXhc[1]=qhdjsd;TXhc[2]=1;
						     TXhc[3]=qhdjsd;TXhc[4]=1;  
						     TXhc[5]=qhdjsd;TXhc[6]=1;  
						     TXhc[7]=qhdjsd;TXhc[8]=1; 
						     break;
					 case KEY_PRESSED_OFFSET_W:     //前进4
						     if(qhbj!=4) qhdjsd=cs_sd;
						     qhbj=4;
						     TXhc[1]=qhdjsd;TXhc[2]=0;
						     TXhc[3]=qhdjsd;TXhc[4]=0;  
						     TXhc[5]=qhdjsd;TXhc[6]=0;  
						     TXhc[7]=qhdjsd;TXhc[8]=0; 
						     break;
					 case KEY_PRESSED_OFFSET_W|KEY_PRESSED_OFFSET_D:   //上右45°5
						     if(qhbj!=5) qhdjsd=cs_sd;
						     qhbj=5; 
						     TXhc[1]=TXhc[7]=0;
						     TXhc[3]=qhdjsd;//2 
						     TXhc[5]=qhdjsd;//3
						     TXhc[2]=TXhc[4]=TXhc[6]=TXhc[8]=0;
						     break;
					 case KEY_PRESSED_OFFSET_W|KEY_PRESSED_OFFSET_A:   //上左45°6
						     if(qhbj!=6) qhdjsd=cs_sd;
						     qhbj=6;
					             TXhc[3]=TXhc[5]=0;
						     TXhc[1]=qhdjsd;//1
						     TXhc[7]=qhdjsd;//4 
					             TXhc[2]=TXhc[4]=TXhc[6]=TXhc[8]=0;
						     break;
					 case KEY_PRESSED_OFFSET_S|KEY_PRESSED_OFFSET_A:   //下左45°7
						     if(qhbj!=7) qhdjsd=cs_sd;
						     qhbj=7;
						     TXhc[1]=TXhc[7]=0;
						     TXhc[3]=qhdjsd;//2 
						     TXhc[5]=qhdjsd;//3
					             TXhc[2]=TXhc[4]=TXhc[6]=TXhc[8]=1;
						     break;
					 case KEY_PRESSED_OFFSET_S|KEY_PRESSED_OFFSET_D:   //下右45°8
						     if(qhbj!=8) qhdjsd=cs_sd;
						     qhbj=8;
					             TXhc[3]=TXhc[5]=0;
						     TXhc[1]=qhdjsd;//1
						     TXhc[7]=qhdjsd;//4
						     TXhc[2]=TXhc[4]=TXhc[6]=TXhc[8]=1;
						     break;
					 case shift+E:
						     TXhc[9]=TXhc[9]|(0x02);
						     break;
					 case ctrl+E:
						     wuxian_sj=kaichuang1;
						     break;
	//				 case KEY_PRESSED_OFFSET_E: 
	//					     break;
	//				 case shift: 
	//					     break;
	//				 case ctrl: 
	//					     break;
					 
					 default:   
						     TXhc[1]=0; TXhc[3]=0;TXhc[5]=0; TXhc[7]=0;qhbj=0;
						     TXhc[9]=TXhc[9]&(0xfd);
						     break;
				}
				if(tmp_buf[6]>1)//右
				{
					if(tmp_buf[6]>djsd)tmp_buf[6]=djsd;
					else tmp_buf[6]=tmp_buf[6];
					switch (qhbj)
					{
						case 1:       // //左平移1
							TXhc[5]=TXhc[7]=djsd;
							TXhc[1]=TXhc[3]=djsd-tmp_buf[6];
							break;
						case 2:       // //右平移2
							TXhc[1]=TXhc[3]=djsd;
							TXhc[5]=TXhc[7]=djsd-tmp_buf[6];
							break;
						case 3:       // //后退3
							TXhc[1]=TXhc[5]=djsd;
							TXhc[3]=djsd-tmp_buf[6];
							TXhc[7]=0;
							break;
						case 4:       // //前进4
							TXhc[3]=TXhc[7]=djsd;
							TXhc[5]=djsd-tmp_buf[6];
							TXhc[1]=0;
							break;
						case 5:     //上右45°5
							TXhc[5]=TXhc[3]=djsd;
							TXhc[7]=tmp_buf[6];
							TXhc[5]=TXhc[5]-tmp_buf[6];
							break;
						case 6:      //上左45°6
							TXhc[1]=TXhc[7]=djsd;
						        TXhc[3]=tmp_buf[6];
							TXhc[1]=TXhc[1]-tmp_buf[6];
							break;
						case 7:       //下左45°7
							TXhc[3]=TXhc[5]=djsd;
							TXhc[1]=tmp_buf[6];
							TXhc[3]=TXhc[3]-tmp_buf[6];
							break;
						case 8:        //下右45°8
							TXhc[7]=TXhc[1]=djsd;
							TXhc[5]=tmp_buf[6];
							TXhc[7]=TXhc[7]-tmp_buf[6];
							break;
						default:      //原地右转
							TXhc[1]=tmp_buf[6];TXhc[2]=1;
							TXhc[3]=tmp_buf[6];TXhc[4]=0;
							TXhc[5]=tmp_buf[6];TXhc[6]=1;
							TXhc[7]=tmp_buf[6];TXhc[8]=0;
							break;
					}
				}
				else if(tmp_buf[6]<-1)//左
				{
					if((tmp_buf[6])<(-djsd))tmp_buf[6]=djsd;
					else tmp_buf[6]=(-tmp_buf[6]);
					switch (qhbj)
					{
						case 1:       // //左平移1
							TXhc[1]=TXhc[3]=djsd;
							TXhc[5]=TXhc[7]=djsd-tmp_buf[6];
							break;
						case 2:       // //右平移2
							TXhc[5]=TXhc[7]=djsd;
							TXhc[1]=TXhc[3]=djsd-tmp_buf[6];
							break;
						case 3:       // //后退3
							TXhc[3]=TXhc[7]=djsd;
							TXhc[1]=djsd-tmp_buf[6];
							TXhc[5]=0;
							break;
						case 4:       // //前进4
							TXhc[5]=TXhc[1]=djsd;
							TXhc[7]=djsd-tmp_buf[6];
							TXhc[3]=0;
							break;
						case 5:     //上右45°5
							TXhc[5]=TXhc[3]=djsd;
							TXhc[1]=tmp_buf[6];
							TXhc[3]=TXhc[3]-tmp_buf[6];
							break;
						case 6:      //上左45°6
							TXhc[1]=TXhc[7]=djsd;
						        TXhc[5]=tmp_buf[6];
							TXhc[7]=TXhc[7]-tmp_buf[6];
							break;
						case 7:       //下左45°7
							TXhc[3]=TXhc[5]=djsd;
							TXhc[7]=tmp_buf[6];
							TXhc[5]=TXhc[5]-tmp_buf[6];
							break;
						case 8:        //下右45°8
							TXhc[7]=TXhc[1]=djsd;
							TXhc[3]=tmp_buf[6];
							TXhc[1]=TXhc[1]-tmp_buf[6];
							break;
						default:      //原地左转
							TXhc[1]=tmp_buf[6];TXhc[2]=0;
							TXhc[3]=tmp_buf[6];TXhc[4]=1;  
							TXhc[5]=tmp_buf[6];TXhc[6]=0;  
							TXhc[7]=tmp_buf[6];TXhc[8]=1;
							break;
					}
				}
			//==============================================================
			//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
			if((tmp_buf[11]&0x40)==0x40){anjian_Q=1;}
			else if(anjian_Q==1&&(tmp_buf[11]&0x40)==0x00)
			{
				anjian_Q=0;
				if(djsd==15)djsd=30;
				else djsd=15;
				if((djsd-cs_sd)/5>1)sdbjbj=(djsd-cs_sd)/5;
				else sdbjbj=1;
			}
			else anjian_Q=0;
			if(tmp_buf[7]>=0){TXhc[11]=tmp_buf[7];TXhc[9]=TXhc[9]&(0xef);}//上下
			else {TXhc[11]=-tmp_buf[7];TXhc[9]=TXhc[9]|(0x10);}
			if(tmp_buf[10]==0)TXhc[9]=TXhc[9]&(0xf7);//停
			else TXhc[9]=TXhc[9]|(0x08);//转  
			if(tmp_buf[9]==0)TXhc[9]=TXhc[9]&(0xfe);//停
			else TXhc[9]=TXhc[9]|(0x01);//射
			read_cs=(read_cs+1)%3;
		}
		else if(tmp_buf[4]==2)//遥控
		{
			if(tmp_buf[3]==0x400&&tmp_buf[2]==0x400&&tmp_buf[0]==0x400&&tmp_buf[1]==0x400){TXhc[1]=0; TXhc[3]=0;TXhc[5]=0; TXhc[7]=0;}//停止
			//==================================================================
			else if(tmp_buf[3]<0x400&&tmp_buf[2]==0x400)//后退
			{
				TXhc[1]=(1024-tmp_buf[3])/20;TXhc[2]=1;
				TXhc[3]=(1024-tmp_buf[3])/20;TXhc[4]=1;  
				TXhc[5]=(1024-tmp_buf[3])/20;TXhc[6]=1;  
				TXhc[7]=(1024-tmp_buf[3])/20;TXhc[8]=1; 			
			}
			else if(tmp_buf[3]>0x400&&tmp_buf[2]==0x400)//前进
			{
				TXhc[1]=(tmp_buf[3]-1024)/20;TXhc[2]=0;
				TXhc[3]=(tmp_buf[3]-1024)/20;TXhc[4]=0;  
				TXhc[5]=(tmp_buf[3]-1024)/20;TXhc[6]=0;  
				TXhc[7]=(tmp_buf[3]-1024)/20;TXhc[8]=0; 
			}
			else if(tmp_buf[3]==0x400&&tmp_buf[2]<0x400)//左平移
			{
				TXhc[1]=(1024-tmp_buf[2])/20;TXhc[2]=0;
				TXhc[3]=(1024-tmp_buf[2])/20;;TXhc[4]=1;
				TXhc[5]=(1024-tmp_buf[2])/20;;TXhc[6]=1;
				TXhc[7]=(1024-tmp_buf[2])/20;;TXhc[8]=0;			
			}
			else if(tmp_buf[3]==0x400&&tmp_buf[2]>0x400)//右平移
			{
				TXhc[1]=(tmp_buf[2]-1024)/20;TXhc[2]=1;
				TXhc[3]=(tmp_buf[2]-1024)/20;TXhc[4]=0;  
				TXhc[5]=(tmp_buf[2]-1024)/20;TXhc[6]=0;  
				TXhc[7]=(tmp_buf[2]-1024)/20;TXhc[8]=1;
			}
			else if(tmp_buf[3]>0x400&&tmp_buf[2]<0x400) //左上
			{
				chasd=(tmp_buf[3]-1024)-(1024-tmp_buf[2]);
				if(chasd<20&&chasd>(-20))
				{
					TXhc[3]=0;TXhc[4]=0;//2 
					TXhc[5]=0;TXhc[6]=0;//3
				}
				else if(chasd>0)
				{
					TXhc[3]=chasd/20;TXhc[4]=0;//2 
					TXhc[5]=chasd/20;TXhc[6]=0;//3
				}
				else if(chasd<0)
				{
					
					TXhc[3]=(-chasd)/20;TXhc[4]=1;//2 
					TXhc[5]=(-chasd)/20;TXhc[6]=1;//3
				}
				TXhc[1]=(tmp_buf[3]-1024)/20;TXhc[2]=0;//1
				TXhc[7]=(tmp_buf[3]-1024)/20;TXhc[8]=0;//4 
				
			}
			else if(tmp_buf[3]<0x400&&tmp_buf[2]<0x400) //左下
			{
				
				chasd=(1024-tmp_buf[3])-(1024-tmp_buf[2]);
				if(chasd<20&&chasd>(-20))
				{
					TXhc[1]=0;TXhc[2]=1;//1
					TXhc[7]=0;TXhc[8]=1;//4
				}
				else if(chasd>0)
				{
					TXhc[1]=chasd/20;TXhc[2]=1;//1
					TXhc[7]=chasd/20;TXhc[8]=1;//4
				}
				else if(chasd<0)
				{
					
					TXhc[1]=(-chasd)/20;TXhc[2]=0;//1
					TXhc[7]=(-chasd)/20;TXhc[8]=0;//4
				}
				TXhc[3]=(1024-tmp_buf[3])/20;TXhc[4]=1;//2 
				TXhc[5]=(1024-tmp_buf[3])/20;TXhc[6]=1;//3
				
			}
			else if(tmp_buf[3]>0x400&&tmp_buf[2]>0x400) //右上
			{
				chasd=(tmp_buf[3]-1024)-(tmp_buf[2]-1024);
				if(chasd<20&&chasd>(-20))
				{
					TXhc[1]=0;TXhc[2]=0;//1
					TXhc[7]=0;TXhc[8]=0;//4
				}
				else if(chasd>0)
				{
					TXhc[1]=chasd/20;TXhc[2]=0;//1
					TXhc[7]=chasd/20;TXhc[8]=0;//4
				}
				else if(chasd<0)
				{
					
					TXhc[1]=(-chasd)/20;TXhc[2]=1;//1
					TXhc[7]=(-chasd)/20;TXhc[8]=1;//4
				}
				TXhc[3]=(tmp_buf[3]-1024)/20;TXhc[4]=0;//2 
				TXhc[5]=(tmp_buf[3]-1024)/20;TXhc[6]=0;//3
			}
			else if(tmp_buf[3]<0x400&&tmp_buf[2]>0x400) //右下
			{
				chasd=(1024-tmp_buf[3])-(tmp_buf[2]-1024);
				if(chasd<20&&chasd>(-20))
				{
					TXhc[3]=0;TXhc[4]=1;//2 
					TXhc[5]=0;TXhc[6]=1;//3
				}
				else if(chasd>0)
				{
					TXhc[3]=chasd/20;TXhc[4]=1;//2 
					TXhc[5]=chasd/20;TXhc[6]=1;//3
				}
				else if(chasd<0)
				{
					
					TXhc[3]=(-chasd)/20;TXhc[4]=0;//2 
					TXhc[5]=(-chasd)/20;TXhc[6]=0;//3
				}
				TXhc[1]=(1024-tmp_buf[3])/20;TXhc[2]=1;//1
				TXhc[7]=(1024-tmp_buf[3])/20;TXhc[8]=1;//4 	
			}
			else{TXhc[1]=0; TXhc[3]=0;TXhc[5]=0; TXhc[7]=0;}
			//=================================================================================================
			hc1=jueduizhi(1024-tmp_buf[0])/30;          //右摇杆左右偏移量
			if((jueduizhi(tmp_buf[3]-1024)==jueduizhi(tmp_buf[2]-1024))&&tmp_buf[0]!=0x400)//原地转
			{
				if(tmp_buf[0]>0x400)//原地右转
				{
					 TXhc[1]=jueduizhi(tmp_buf[0]-1024)/20;TXhc[2]=1;
					 TXhc[3]=jueduizhi(tmp_buf[0]-1024)/20;TXhc[4]=0;
					 TXhc[5]=jueduizhi(tmp_buf[0]-1024)/20;TXhc[6]=1;
					 TXhc[7]=jueduizhi(tmp_buf[0]-1024)/20;TXhc[8]=0; 
				}
				else if(tmp_buf[0]<0x400)//原地左转
				{
					 TXhc[1]=jueduizhi(tmp_buf[0]-1024)/20;TXhc[2]=0;
					 TXhc[3]=jueduizhi(tmp_buf[0]-1024)/20;TXhc[4]=1;  
					 TXhc[5]=jueduizhi(tmp_buf[0]-1024)/20;TXhc[6]=0;  
					 TXhc[7]=jueduizhi(tmp_buf[0]-1024)/20;TXhc[8]=1;
				}
					
			}
			else if((tmp_buf[0]!=0x400)&&(jueduizhi(1024-tmp_buf[2])>jueduizhi(1024-tmp_buf[3]))&&(tmp_buf[2]>0x400))//右平移
			{
				TXhc[1]=(tmp_buf[2]-1024)/20;TXhc[2]=1;
				TXhc[3]=(tmp_buf[2]-1024)/20;TXhc[4]=0;  
				TXhc[5]=(tmp_buf[2]-1024)/20;TXhc[6]=0;  
				TXhc[7]=(tmp_buf[2]-1024)/20;TXhc[8]=1;
				if(tmp_buf[0]>0x400)//右
				{
					TXhc[1]=TXhc[1]+hc1;
					TXhc[3]=TXhc[3]+hc1;
					if(TXhc[1]>=33)TXhc[1]=33;
					if(TXhc[3]>=33)TXhc[3]=33;
					
				}
				else if(tmp_buf[0]<0x400)//左
				{
					TXhc[5]=TXhc[5]+hc1;
					TXhc[7]=TXhc[7]+hc1;
					if(TXhc[5]>=33)TXhc[5]=33;
					if(TXhc[7]>=33)TXhc[7]=33;
				}
			}
			else if((tmp_buf[0]!=0x400)&&(jueduizhi(1024-tmp_buf[2])>jueduizhi(1024-tmp_buf[3]))&&(tmp_buf[2]<0x400))//左平移
			{
				TXhc[1]=(1024-tmp_buf[2])/20;TXhc[2]=0;
				TXhc[3]=(1024-tmp_buf[2])/20;;TXhc[4]=1;
				TXhc[5]=(1024-tmp_buf[2])/20;;TXhc[6]=1;
				TXhc[7]=(1024-tmp_buf[2])/20;;TXhc[8]=0;
				if(tmp_buf[0]>0x400)//右
				{
					TXhc[5]=TXhc[5]+hc1;
					TXhc[7]=TXhc[7]+hc1;
					if(TXhc[5]>=33)TXhc[5]=33;
					if(TXhc[7]>=33)TXhc[7]=33;
				}
				else if(tmp_buf[0]<0x400)//左
				{
					TXhc[1]=TXhc[1]+hc1;
					TXhc[3]=TXhc[3]+hc1;
					if(TXhc[1]>=33)TXhc[1]=33;
					if(TXhc[3]>=33)TXhc[3]=33;
				}
			}
			else if((tmp_buf[0]!=0x400)&&(jueduizhi(1024-tmp_buf[2])<jueduizhi(1024-tmp_buf[3]))&&(tmp_buf[3]>0x400))//上
			{
				TXhc[1]=(tmp_buf[3]-1024)/20;TXhc[2]=0;
				TXhc[3]=(tmp_buf[3]-1024)/20;TXhc[4]=0;  
				TXhc[5]=(tmp_buf[3]-1024)/20;TXhc[6]=0;  
				TXhc[7]=(tmp_buf[3]-1024)/20;TXhc[8]=0; 
				if(tmp_buf[0]>0x400)//右
				{
					if(hc1<=11){TXhc[1]=TXhc[5]=0;}
					else
					{
						if(TXhc[1]>15){TXhc[1]=TXhc[3]/2;TXhc[5]=0;}
						else {TXhc[1]=0;TXhc[6]=1;}	
					}
				}
				else if(tmp_buf[0]<0x400)//左
				{
					if(hc1<=11){TXhc[3]=TXhc[7]=0;}
					else 
					{
						if(TXhc[1]>15){TXhc[3]=TXhc[1]/2;TXhc[7]=0;}
						else {TXhc[3]=0;TXhc[8]=1;}
					}
				}
			}
			else if((tmp_buf[0]!=0x400)&&(jueduizhi(1024-tmp_buf[2])<jueduizhi(1024-tmp_buf[3]))&&(tmp_buf[3]<0x400))//下
			{
				TXhc[1]=(1024-tmp_buf[3])/20;TXhc[2]=1;
				TXhc[3]=(1024-tmp_buf[3])/20;TXhc[4]=1;  
				TXhc[5]=(1024-tmp_buf[3])/20;TXhc[6]=1;  
				TXhc[7]=(1024-tmp_buf[3])/20;TXhc[8]=1; 
				if(tmp_buf[0]>0x400)//右
				{
					if(hc1<=11){TXhc[3]=TXhc[7]=0;}
					else
					{
						if(TXhc[1]>15){TXhc[3]=TXhc[1]/2;TXhc[7]=0;}
						else {TXhc[7]=0;TXhc[4]=1;}	
					}
				}
				else if(tmp_buf[0]<0x400)//左
				{
					if(hc1<=11){TXhc[1]=TXhc[5]=0;}
					else
					{
						if(TXhc[1]>15){TXhc[1]=TXhc[3]/2;TXhc[5]=0;}
						else {TXhc[5]=0;TXhc[2]=1;}	
					}
				}
			}
			//=======================================================
			if(tmp_buf[1]>0x400){TXhc[11]=(tmp_buf[1]-1024)/35;TXhc[9]=TXhc[9]|(0x10);}//上下
			else if(tmp_buf[1]<0x400){TXhc[11]=(1024-tmp_buf[1])/35;TXhc[9]=TXhc[9]&(0xef);}
			if(tmp_buf[5]==1)TXhc[9]=TXhc[9]|(0x08);//转 
			else  TXhc[9]=TXhc[9]&(0xf7);//停
			if(tmp_buf[5]==2)TXhc[9]=TXhc[9]|(0x01);//射
			else  TXhc[9]=TXhc[9]&(0xfe);//停
		 } 
}
void XIE_sz(void)
{
	u8 ii;
	for(ii=0;ii<9;ii++){
	u2_RX=xie;
	USART_SendData(USART2,TXhc[ii]);
	while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
	u2_RX=du;
	}
	u2_RX=xie;
	USART_SendData(USART2,TXhc[12]);
	while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
	u2_RX=du;
}
/*=====================================================================

		         DMA中断

======================================================================*/
u8 d_zd=0;
void DMA1_Channel3_IRQHandler(void)
{
    if(DMA_GetITStatus(DMA1_IT_TC3))
    {  
	 DMA_ClearFlag(DMA1_IT_TC3);
	 d_zd=(d_zd+1)%20;
	 if(d_zd<=10) LED2=1;
	 else LED2=0;
         tmp_buf[0] =shuju.rc.ch0=( Dbus_rx_buffer[0]| (Dbus_rx_buffer[1] << 8)) & 0x07ff; //!< Channel 0
         tmp_buf[1] = shuju.rc.ch1=((Dbus_rx_buffer[1] >> 3) | (Dbus_rx_buffer[2] << 5)) & 0x07ff; //!< Channel 1
	 tmp_buf[2]= shuju.rc.ch2=((Dbus_rx_buffer[2] >> 6) | (Dbus_rx_buffer[3] << 2) | (Dbus_rx_buffer[4] << 10)) & 0x07ff;//!< Channel 2
         tmp_buf[3]=shuju.rc.ch3=((Dbus_rx_buffer[4] >> 1) | (Dbus_rx_buffer[5] << 7)) & 0x07ff; //!< Channel 3

         tmp_buf[4] = shuju.rc.s1=((Dbus_rx_buffer[5] >> 4)& 0x000C) >> 2; //!< Switch left
         tmp_buf[5] =shuju.rc.s2=((Dbus_rx_buffer[5] >> 4)& 0x0003);  //!< Switch right
	    
         tmp_buf[6] =shuju.mouse.x= Dbus_rx_buffer[6] | (Dbus_rx_buffer[7]  << 8); //!< Mouse X axis
         tmp_buf[7] =shuju.mouse.y= Dbus_rx_buffer[8] | (Dbus_rx_buffer[9]  << 8); //!< Mouse Y axis
         tmp_buf[8]=shuju.mouse.z= Dbus_rx_buffer[10] | (Dbus_rx_buffer[11] << 8); //!< Mouse Z axis
	    
         tmp_buf[9] =shuju.mouse.press_l =Dbus_rx_buffer[12];  //!< Mouse Left Is Pressed
         tmp_buf[10]= shuju.mouse.press_r=Dbus_rx_buffer[13];  //!< Mouse Right Is Pressed
	    
         tmp_buf[11]=shuju.key.v= Dbus_rx_buffer[14] | (Dbus_rx_buffer[15]  << 8); //!< KeyBoard value
	 if(kaiji_jiesuo==0&&tmp_buf[4]==2&&tmp_buf[5]==2)kaiji_jiesuo=1;
	 if(kaiji_jiesuo==1)
	 {
		 baohu=bhbj=0;
		 jianpan();
		 XIE_sz(); 
	 }
    }
}

